package com.smallA.controller;

import com.alibaba.fastjson.JSON;
import com.smallA.constant.AccessConstant;
import com.smallA.constant.DataConstant;
import com.smallA.po.Grade;
import com.smallA.service.ExamService;
import com.smallA.service.GradeService;
import com.smallA.service.TestService;
import com.smallA.utils.AutoSubmit;
import com.smallA.utils.JwtUtils;
import com.smallA.vo.Result;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 君未洋
 */
@RestController
public class StudentController {


    @Resource
    private GradeService gradeService;
    @Resource
    private TestService testService;
    @Resource
    private ExamService examService;


    /**
     * 通过考试id返回剩余的考试时间的毫秒值
     */
    @Secured({AccessConstant.STUDENT_ACCESS, AccessConstant.TEACHER_ACCESS})
    @RequestMapping("/Student/getEndTime")
    public Result getEndTime(@RequestBody Map<String, Integer> map) {
        Integer gid = map.get("gid");
        if (gid == null) {
            return null;
        }
        /*因为每个gid都是有对应的一个基本考试,所以可以使用redis来存放考试时间之类的玩意*/
        Grade grade = gradeService.selectGradeById(gid);
        // return (grade.getBeginTime().getTime() + grade.getTest().getTestTime() * 1000 * 60 - System.currentTimeMillis());
        return null;
    }

    /**
     * 通过学生id和试卷id报名参加考试
     */
    @RequestMapping("/Student/joinExam")
    @Secured(AccessConstant.STUDENT_ACCESS)
    public Object joinExam(@RequestBody Map<String, Integer> map) {
        return gradeService.insertGrade(JwtUtils.getUser().getId(), map.get("tid"));
    }

    /**
     * 开始考试用的接口
     */
    @Secured(AccessConstant.STUDENT_ACCESS)
    @RequestMapping("/Student/beginExam")
    public Object beginExam(@RequestBody Map<String, Integer> ids) {
        Integer gid = ids.get(DataConstant.GRADE_ID);
        if (gradeService.BeginGrade(gid)) {
            //获取考试信息上传
            Map<String, Object> map = new HashMap<>();
            map.put(DataConstant.EXAM_DETAIL, examService.selectExamByGradeId(gid));
            map.put(DataConstant.ID_GRADE, gid);
            return map;
        } else {
            return false;
        }
    }

    @RequestMapping("/Student/beginTest")
    @Deprecated
    public Object beginTest(@RequestBody Map<String, Integer> map) {
        int id = JwtUtils.getUser().getId();
        return gradeService.beginTest(map.get("tid"), id).getData();
    }

    /**
     * 提交试卷用的接口
     */
    @RequestMapping("/Student/winding")
    @Secured(AccessConstant.STUDENT_ACCESS)
    public Object endExam(@RequestBody Map<String, Object> answers) {
        int gid = Integer.parseInt(answers.remove(DataConstant.GRADE_ID).toString());
        if (testService.windingTest(answers, gid)) {
            return gradeService.EndGrade(gid);
        } else {
            return false;
        }
    }


    @RequestMapping("/myTest/wind")
    public Object testWinding(@RequestBody Map<String, String> answers) {
        System.out.println(answers);
        return "OK";
    }

    /**
     * 通过学生的id获取学生对应的考试情况
     */
    @RequestMapping("/Student/getGrade")
    public Object getGrade() {
        return gradeService.selectGradesById(JwtUtils.getUser().getId());
    }

    /**
     * 获取考试的详细信息
     */
    @RequestMapping("/Student/getExam")
    @Secured(AccessConstant.STUDENT_ACCESS)
    public Object getExam(@RequestBody Map<String, Integer> map) {
        return examService.selectExamById(map.get(DataConstant.GRADE_ID)).getData();
    }


    /**
     * 邮件测试用类
     */
    @RequestMapping("/Student/task")
    public String taskEmail(String address, Integer time) {
        AutoSubmit autoSubmit = new AutoSubmit(time, address);
        if (address != null) {
            autoSubmit.setMsg("亲爱的用户,您发送的测试邮件已到达");
            new Thread(autoSubmit).start();
        }
        return "Message sending";
    }


    @RequestMapping("/Test")
    public Object Test(@RequestBody Map<String, Object> map) {
        List<String> username = JSON.parseArray(map.get("username").toString(), String.class);
        return username;
    }

}
